//
// Created by Suzhen on 2017/2/24.
//

#ifndef ORIONV2_IOBUFFER_H
#define ORIONV2_IOBUFFER_H

#include "Orion.h"
#include "FutureAdapt.hpp"

ORION_V2_NAMESPACE_BEGIN

        class IOBuffer final {

        public:

            IOBuffer(const RawDataType *bf, size_t sz, bool transfer = NO);

            IOBuffer(const IOBuffer &) = delete;

            IOBuffer &operator=(const IOBuffer &other) = delete;

            IOBuffer(IOBuffer &&other);

            IOBuffer &operator=(IOBuffer &&other);

            ~IOBuffer();

            Future<bool> getCompleteFuture();

        private:
            /* 如果为YES,则表示使用用户的赋值数据,DataBufferItem不负责释放
             * 反之，将会产生拷贝操作,且内存由DataBufferItem负责管理,
             * 默认行为是拷贝
             */
            bool transfer;
        public:
            size_t getLen() const;

            size_t getIndex() const;

            const RawDataType *getBuf() const;

            /**
             * buffer此时不会去释放该段内存
             * 前提 构造时，一定要是用户transfer进来的
             */
            void releaseBuf();

        public:
            size_t len;
            size_t index;

            const RawDataType *buf;

            //用于通知该BufferItem发送是否完成
            Promise<bool> *completePromise_;

        };

ORION_V2_NAMESPACE_END


#endif //ORIONV2_IOBUFFER_H
